namespace Warehouse

open System
open System.Windows.Data
open FSharp.Windows
open Warehouse.Controls

type ClientEvents = 
    | Edit
    | Save
    | ClientSelected
    | BrandSelected
    | ProductSizeFilter of (ClientProductCommissionModel -> bool)
    | Refresh

type ClientView(control) as this = 
    inherit PartialView<ClientEvents, ClientModel, ClientControl>(control)
    
    let items = this.Control.Items
    let brandCommission = this.Control.BrandCommission
    let allSize, regularSize, litrSize = this.Control.All, this.Control.Regular, this.Control.Litr

    do
        items.SynchronizedWithCurrentItem()
        brandCommission.SynchronizedWithCurrentItem()

    override this.EventStreams = 
        [
            this.Control.Save.ClickToValue Save
            this.Control.Refresh.ClickToValue Refresh

            items.SelectionChanged |> Observable.mapTo ClientSelected 
            brandCommission.SelectionChangedAndNotNull() |> Observable.mapTo BrandSelected 
            items.DoubleClickRowEvent() |> Observable.mapTo Edit

            allSize.ClickToValue(ProductSizeFilter(fun _ -> true))
            regularSize.ClickToValue(ProductSizeFilter(fun x -> x.Product.IsRegularSize()))
            litrSize.ClickToValue(ProductSizeFilter(fun x -> x.Product.IsLitrSize()))
        ]

    override this.SetBindings model =
        Binding.FromExpression 
            <@
                this.Control.ProductSizeFilter.IsEnabled <- model.RegularOrLitrSizeFilterEnabled
                allSize.IsChecked <- model.SelectedClient.SelectedBrand.AllSizesFilter
                regularSize.IsChecked <- model.SelectedClient.SelectedBrand.RegularSizeFilter
                litrSize.IsChecked <- model.SelectedClient.SelectedBrand.LitrSizeFilter
            @>

        items.SetBindings(
            model, 
            itemsSource = <@ model.Items @>,
            selectedItem = <@ model.SelectedClient @>,
            columnBindings = fun item -> 
                [
                    this.Control.ItemsClientId, <@@ item.Id @@>
                    this.Control.ItemsCompanyName, <@@ item.CompanyName @@>
                    this.Control.ItemsTotalSales, <@@ String.Format("{0:c}", item.TotalSales) @@>
                ]
        )

        brandCommission.SetBindings(
            model,
            itemsSource = <@ model.SelectedClient.BrandCommissions @>,
            selectedItem = <@ model.SelectedClient.SelectedBrand @>,
            columnBindings = fun item ->
                [
                    this.Control.BrandCommissionBrand, <@@ item.Brand.Name @@>
                    this.Control.BrandCommissionDefaut, <@@ item.Default @@>
                    this.Control.BrandCommissionRegularProducts, <@@ item.RegularSizeCommission @@>
                    this.Control.BrandCommissionLitSizeProducts, <@@ item.LitrSizeCommission @@>
                ]
        )

        this.Control.ProductCommission.SetBindings(
            model,
            itemsSource = <@ model.SelectedClient.SelectedBrand.ProductCommissions @>,
            columnBindings = fun item ->
                [
                    this.Control.ProductPctTitle, <@@ item.Title @@>
                    this.Control.ProductPctValue, <@@ item.Value @@>
                ]
        )
